/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package brickroadfitness;

import java.util.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;


public class ListBroken extends JFrame implements ActionListener
{
    public Statement stmt1;
    public ResultSet rset;
    public String query1;
    private JLabel header;
    private JScrollPane scroll;
    private JLabel listlabel;
    private JPanel listpanel;
    private JButton ok;
    private JTable table;

    public ListBroken()
    {
        super("Brickroad Fitness - List Broken Equipments");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBackground(new Color(255, 255, 102));
        setResizable(false);
        setVisible(true);
        //setLocationRelativeTo(null);
        setForeground(new Color(255, 255, 102));
        initComponents();
    }

    private void initComponents() {
        
        cnd a = new cnd();
            try {
                a.connect();
                query1 = "SELECT eqID, eqName, eqPrice, eqStatus FROM equipment e WHERE e.eqCondition = 'Broken' ORDER BY `eqID`";
                stmt1 = a.con1.createStatement();
                rset = stmt1.executeQuery(query1);
                ResultSetMetaData md = rset.getMetaData();
                int columnCount = md.getColumnCount();

                Vector columns = new Vector(columnCount);

                //store column names
                //for(int i=1; i<=columnCount; i++)
                //    columns.add(md.getColumnName(i));

                columns.add("Equipment ID");
                columns.add("Equipment Name");
                columns.add("Price");
                columns.add("Status");

                Vector data = new Vector();
                Vector row;

                //store row data
                while(rset.next())
                {
                    row = new Vector(columnCount);
                    for(int i=1; i<=columnCount; i++)
                        row.add(rset.getString(i));
                    data.add(row);
                }
                System.out.println("Records from member table were successfully copied.");
                table = new JTable(data, columns) {
                    public boolean isCellEditable(int row, int column) {
                        //all cells false
                        return false;
                    }
                };
                a.disconnect();
            }
            catch (Exception ex)
            {
                JOptionPane invalidsearch = new JOptionPane();
                invalidsearch.showMessageDialog(listpanel, "Error copying records from equipment table. " + ex.getMessage());
                System.out.println("Error copying records from member table. " + ex.getMessage());
            }

        header = new JLabel();
        listpanel = new JPanel();
        listlabel = new JLabel();
        scroll = new JScrollPane();
        ok = new JButton();

        ok.addActionListener(this);

        header.setIcon(new ImageIcon(getClass().getResource("/PRIMER.jpg"))); // NOI18N
        header.setText("Brickroad Fitness");

        listpanel.setBackground(new Color(255, 255, 102));
        listpanel.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0), 5));

        listlabel.setFont(new Font("Arial", 3, 18)); // NOI18N
        listlabel.setText("List Broken Equipments");

        table.setFont(new Font("Tahoma", 1, 12)); // NOI18N
       /* table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {1, "Proteus MTM 7520 Motorized Treadmill", 75500, "Broken", "Not Used"},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
//                {null, null, null, null, null},
//                {null, null, null, null, null},
//                {null, null, null, null, null},
//                {null, null, null, null, null}
            },
            new String [] {
                "Equipment ID", "Equipment Name", "Price", "Condition", "Status"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.Float.class, java.lang.Integer.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });*/
        table.setGridColor(new Color(0, 0, 0));
        table.setRowHeight(20);
        table.getTableHeader().setReorderingAllowed(false);
        scroll.setViewportView(table);

        ok.setFont(new Font("Century Gothic", 1, 14)); // NOI18N
        ok.setText("OK");
        ok.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        GroupLayout listpanelLayout = new GroupLayout(listpanel);
        listpanel.setLayout(listpanelLayout);
        listpanelLayout.setHorizontalGroup(
            listpanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(listpanelLayout.createSequentialGroup()
                .addGroup(listpanelLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                    .addGroup(listpanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(ok, GroupLayout.PREFERRED_SIZE, 83, GroupLayout.PREFERRED_SIZE))
                    .addGroup(GroupLayout.Alignment.LEADING, listpanelLayout.createSequentialGroup()
                        .addGap(36, 36, 36)
                        .addComponent(listlabel))
                    .addGroup(GroupLayout.Alignment.LEADING, listpanelLayout.createSequentialGroup()
                        .addGap(72, 72, 72)
                        .addComponent(scroll, GroupLayout.PREFERRED_SIZE, 768, GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(45, Short.MAX_VALUE))
        );
        listpanelLayout.setVerticalGroup(
            listpanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(listpanelLayout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(listlabel)
                .addGap(18, 18, 18)
                .addComponent(scroll, GroupLayout.PREFERRED_SIZE, 363, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(ok, GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
                .addContainerGap())
        );

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(header, 0, 0, Short.MAX_VALUE)
            .addComponent(listpanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(header)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(listpanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pack();
    }

    public void actionPerformed(ActionEvent e)
    {
        Object event = new Object();
        event = e.getSource();

        if (event == ok)
        {
            dispose();
        }
    }
}
